#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-09-21 11:29 上午
# @Author  : yangdy
# @File    : 2020-09-21.538.把二叉搜索树转换为累加树
# @Software: PyCharmCE
from tools.my_methods import *


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def convertBST(self, root: TreeNode) -> TreeNode:
        # display(root)

        self.a = 0

        def dfs(rt):
            if not rt:
                return
            dfs(rt.right)
            self.a += rt.val
            rt.val = self.a
            dfs(rt.left)

        dfs(root)
        # display(root)
        return root


questions = [
    ([5, 2, 13],),
    ([2, 0, 3, -4, 1],),
]

work(questions, lambda q: Solution().convertBST(mk_tree(q[0], TreeNode)))
